package com.dcms.admin.controller;

import com.dcms.admin.bean.Article;
import com.dcms.admin.service.IArticleService;
import com.dcms.util.layui.LayTable;
import com.dcms.util.ResultBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: baochao
 * @Date 2018/2/12 0012
 * @PROJECT_NAME vso
 */
@Controller
@RequestMapping("admin/article")
public class ArticleController extends BaseController {

    @Autowired
    private IArticleService articleService;

    @RequestMapping("list")
    public String list() {
        return "admin/article/list";
    }

    /**
     * 分页查询数据
     *
     * @param request
     * @param param
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping("query")
    @ResponseBody
    public LayTable<Article> query(
            HttpServletRequest request,
            String param,
            Integer limit,
            Integer page
    ) {
        page = page == null ? 1 : page;
        limit = limit == null ? 12 : limit;

        PageHelper.startPage(page, limit);
        List<Article> list = articleService.selectAll(param);
        PageInfo<Article> info = new PageInfo<Article>(list);
        LayTable<Article> bean = new LayTable<Article>(info.getList(), (int) info.getTotal());
        return bean;
    }

    /**
     * 添加 article
     *
     * @param article
     * @return
     */
    @RequestMapping("addArticle")
    public ResultBean addArticle(Article article) {
        ResultBean resultBean = new ResultBean();
        try {
            Integer result = this.articleService.insertSelective(article);
            resultBean.setCode(result);
            resultBean.setObject(article);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultBean;
    }

    /**
     * 编辑 article
     *
     * @param article
     * @return
     */
    @RequestMapping("updateArticle")
    public ResultBean updateArticle(Article article) {
        ResultBean resultBean = new ResultBean();
        try {
            Integer result = this.articleService.updateByPrimaryKeySelective(article);
            resultBean.setCode(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultBean;
    }

    /**
     * 根据articleIds 批量删除article
     * @param articleIds
     * @return
     */
    @RequestMapping("deleteBatchArticle")
    public ResultBean deleteBatchArticle(List<String> articleIds){
        ResultBean resultBean = new ResultBean();
        try{
            Integer result =this .articleService.deleteByPrimaryKeyBatch(articleIds);
            resultBean.setCode(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  resultBean;
    }
}
